#include "uart.h"

#define FOSC 24000000UL
#define BRT  (65536 - FOSC / 115200 / 4)

bit busy;
char wptr;
char rptr;
char buffer[16];

void UartIsr() interrupt 4
{
    if (TI) {
        TI   = 0;
        busy = 0;
    }
    if (RI) {
        RI             = 0;
        buffer[wptr++] = SBUF;
        wptr &= 0x0f;
    }
}

void UartInit()
{
    SCON = 0x50;
    T2L  = BRT;
    T2H  = BRT >> 8;
    AUXR |= 0x15;
    wptr = 0x00;
    rptr = 0x00;
    busy = 0;

    ES = 1;
    EA = 1;
}

void UartSend(char dat)
{
    while (busy)
        ;
    busy = 1;
    SBUF = dat;
}

void UartSendStr(char *p)
{
    while (*p) {
        UartSend(*p++);
    }
}

void SEND_U16(uint num) // ???????
{

    SBUF = num >> 8;
    busy = 1;
    while (busy)
        ;
    SBUF = num;
    busy = 1;
    while (busy)
        ;
}

void freshUart()
{
    if (rptr != wptr) {
        UartSend(buffer[rptr++]);
        rptr &= 0x0f;
    }
}

// void main()
// {

//     UartInit();
//     ES = 1;
//     EA = 1;
//     UartSendStr("Uart Test !\r\n");

//     while (1) {
//         if (rptr != wptr) {
//             UartSend(buffer[rptr++]);
//             rptr &= 0x0f;
//         }
//     }
// }
